<!--suppress ALL -->
<template>
  <div class="page inspectRecords-page" data-page="inspectRecords" style="z-index: 2;">
    <div class="link-page-nav">
        <a href="#" class="link" @click="$root.backToTab()">
            <i class="iconfont icon-zuo"></i>
        </a>
    </div>
    <div class="link-page-top">
        <div class="link-page-title">归档记录</div>
        <a class="link popover-open" id="hidden_select_menu_{{getNewId}}" data-popover="#troubleHidden_menu_list_{{getNewId}}">
            <i id="date_select2_{{getNewId}}"  class="iconfont icon-APPicon_shaixuan1
" style="font-size: 23px;"></i>
        </a>
    </div>
    <div class="page-content checkApply-task-list1 infinite-scroll-content ptr-content"
        @ptr:refresh="refreshCurrentList" 
        @infinite="loadMoreInspectRecordsList"
        data-infinite-distance="50" >
        <div class="ptr-preloader" style="top: -37px;">
            <div class="preloader color-multi"></div>
            <div class="ptr-arrow"></div>
        </div>
        <div class="list media-list" style="margin: 0;">
            <ul id="history-check-content_{{getNewId}}"></ul>
            <div id="inspectRecords_preloader_{{getNewId}}" class="preloader infinite-scroll-preloader" ></div>
        </div>
    </div>
    <div class="popover select-option-con" id="troubleHidden_menu_list_{{getNewId}}">
        <div class="popover-angle"></div>
        <div class="popover-inner">
            <div class="list">
                <ul>
                  <li><a href="" class="list-button item-link popover-close" id="select_hidden_org_{{getNewId}}">选择单位</a></li>
                  <li><a href="" class="list-button item-link popover-close" @click="getTbCompleteInfo" id="select_hidden_isComplete_{{getNewId}}">是否完成</a></li>
                  <li><a href="" class="list-button item-link popover-close" @click="getTbQualifiedInfo" id="select_hidden_isQualified_{{getNewId}}">是否合格</a></li>
                  <li><a href="" class="list-button item-link popover-close" @click="getTbTimeInfo" id="hidden_date_inspect_{{getNewId}}">选择日期</a></li>
                  <li><a href="" class="list-button item-link popover-close removeScreen select-active" @click="removeScreen" >清&nbsp&nbsp&nbsp&nbsp&nbsp空</a></li>
                </ul>
            </div>
        </div>
    </div>
 </div>
</template>
<script>
    return {
        mounted() {
            var self = this;
            if(self.$route.params.dateTime){
              self.date =self.$route.params.dateTime;
            }else{
              self.date =new Date();
            }
        },
        data: function () {
            return {
                orgId:userInfor.org.id,
                // dateValus:['全部时间','今天','三天','七天','一个月','三个月'],
                view_status: self.view_status?self.view_status:2,//2待领取 3执行中 4已完成 5已过期
                status:2,//0:所有 1：未发布 2：已发布(待领取) 3：巡检中 4：巡检完成 5：已过期 6：已重新巡检
                fbsId:0,//消防建筑物id
                areaId:0,
                check_point_array : [], //多选时候保存点位id的数组
                visualCode:'',
                selectContent:'',//搜索内容
                dateTime: "",//开始时间
                isInfinite:false,
                isOpenMore:false,
                complete:0,
                qualified:0,
                orgIds:userInfor.organizationIds,
                selectOptionIndex:'0',
                selectOptionIndex1:"",
                selectOptionIndex2:"",
                selectOptionOrg:"",
                historyIndex:0,
                historySize:10,
                allowInfinite: true,
                getNewId: (new Date()).getTime()
            }
        },
        methods: {
            // backToHome:function(){
            //     // if (app.view.get("#todayInspect")) {
            //     //      app.view.get("#todayInspect").router.currentPageEl.f7Component.showInspectCount();
            //     // }
            //     app.methods.backToTab();
            // },
            getHistoryCheckData:function(){
                let self = this;
                //筛选按钮有值变色
				if(jQuery("#select_hidden_org_" + self.getNewId)[0].innerText != "选择单位" || jQuery("#select_hidden_isComplete_" + self.getNewId)[0].innerText != "是否完成"  || jQuery("#select_hidden_isQualified_" + self.getNewId)[0].innerText != "是否合格"  || jQuery("#hidden_date_inspect_" + self.getNewId)[0].innerText != "选择日期"){
                    jQuery("#date_select2_" + self.getNewId).addClass("select-active");
                }else{
                    jQuery("#date_select2_" + self.getNewId).removeClass("select-active");
                }
                //获取历史巡检数据
                Dao.getInspectTaskHistory({
                  userName:userInfor.accountName,
                  deviceId:appKeyObj.deviceId,
                  orgIds:self.orgIds,
                  status:self.qualified,//0:全部1:合格2：有不合格
                  result:self.complete,//0：全部1未完成，2已完成
                  date:self.dateTime,
                  pageIndex:self.historyIndex,
                  pageSize:self.historySize,
                  pointName:self.selectContent
                },function(data,total) {
                    if(self.historyIndex==0){
                        $("#history-check-content_" + self.getNewId).empty();
                    }
                    if(data&&data.length>0){
                        if(total < self.historySize){
                            $('#inspectRecords_preloader_' + self.getNewId).hide();
                            self.allowInfinite=false;
                        }else{
                            $('#inspectRecords_preloader_' + self.getNewId).show();
                            self.allowInfinite=true;
                        }
                        for(var i=0;i<data.length;i++ ){
                            pointStatusHtml="";
                            var orgHtml="";
                            if(data[i].result==1){
                                pointStatusHtml="<div class='inspectRecords-uncomplete'>未完成</div>"
                            }else{
                                pointStatusHtml="<div class='inspectRecords-complete'>完成</div>"
                            }
                            if(userInfor.isMuilUnitUser==1){
                                orgHtml= '<li class="inspect-list-selected-point">所&nbsp;属&nbsp;单&nbsp;位<span class="inspect-list-belong-unit" >'+common.transNullundefinedToline(data[i].orgName)+'</span></li>';
                            }
                            $("#history-check-content_" + self.getNewId).append(`
                                <li class="inspectRecords-inspect-list accordion-item" id="task-list-`+ (data[i].id+ '_' + self.getNewId) + `">
                                    <a href="#" class="item-content item-link" style="border-bottom: 1px solid rgba(225, 225, 225, 0.5);">
                                        <div class="item-inner">
                                            <div class="item-title" style="font-size: 17px;color: #333;margin-bottom: 8px;font-weight: bold;white-space:normal;">
                                                `+self.transIsFinish(data[i].result)+`
                                                `+common.transNullundefinedToline(data[i].taskName)+`
                                            </div>
                                            <div class="item-footer" style="font-size: 14px;margin-bottom: 2px;">共有巡检点位`+data[i].totalPointNum+`个</div>
                                            <div class="item-footer" style="font-size: 14px;margin-bottom: 2px;">未完成点位`+data[i].unfinishedPointNum+`个</div>
                                            <div class="item-footer" style="font-size: 14px;">不合格点位`+data[i].unQualifiedPointNum+`个</div>
                                            <div class="item-footer" style="font-size: 14px;"><b>开始时间</b>：`+data[i].startTime+`</div>
                                        </div>
                                    </a>
                                    <div class="accordion-item-content" id="taskList-`+ (data[i].id+ '_' + self.getNewId) + `"></div>
                            <li/>`);   
                            // 点击查询明细
                            jQuery("#task-list-" + data[i].id + "_" + self.getNewId).on('click', {'taskId':data[i].id}, function(e) {
                                var taskId = e.data.taskId;
                                var $content = jQuery("#taskList-"+ e.data.taskId +"_"+self.getNewId)
                                
                                if(jQuery(e.currentTarget).hasClass("accordion-item-opened") && $content.text().length < 1){
                                    // 若第一次展开子集，重设height同时查询数据
                                    $content.css('height', 'auto');
                                    // 获取明细
                                    self.getInspectDetail(taskId);
                                } else {
                                    return;
                                }
                            });  
                        }
                        
                        // jQuery(".inspect-list-hasDropDown-ul").off().click(function(e) {
                        //   jQuery(e.currentTarget).parent(".newcheckApply-inspect-list").toggleClass("show");
                        //   jQuery(e.currentTarget).siblings(".checkApplyList-dropdown").slideToggle();
                        // }); 
                    }else{
                        self.allowInfinite=false;
                        $('#inspectRecords_preloader_' + self.getNewId).hide();
                        if (self.historyIndex == 0) { //暂无数据
                            $("#history-check-content_" + self.getNewId).append(`<div class="no-data-img"><img src="`+listNoDataPic()+`"/></div>`);
                        }
                    }
                });
            },
            // 获取点位明细
            getInspectDetail(taskId) {
                let self = this;
                common.loading(1);
                Dao.inspectPointResult({
                    name: '', // 这个字段必传，但是这里不需要
                    taskId: taskId
                }, function(data,total) {
                    if(data&&data.length>0){
                        var pointList=data;//领取的点位
                
                        for(var j=0;j<pointList.length;j++ ){
                            var color="#62CC88";
                            if(pointList[j].pointResultDesc=="合格"){
                                color="#62CC88";
                            }else if(pointList[j].pointResultDesc=="全部不合格"){
                                color="#ED6C6C";
                            }else if(pointList[j].pointResultDesc=="未巡检"){
                                color="#9A9A9A";
                            }else{
                                color="#F8B728";
                            }
                            
                            if(pointList[j].pointResultDesc=="合格"){
                                $("#isOk-"+pointList[j].id).css("background","#69BA71");
                            }else if(pointList[j].pointResultDesc=="全部不合格"){
                                $("#isOk-"+pointList[j].id).css("background","#F02020");
                            }else if(pointList[j].pointResultDesc=="未巡检"){
                                $("#isOk-"+pointList[j].id).css("background","#9A9A9A");
                            }else{
                                $("#isOk-"+pointList[j].id).css("background","#F8B728");
                            }

                            var image = staticPath+"img/emptyImg/default_check.png";
                            if (pointList[j].picUrl&&pointList[j].picUrl.split(',').length > 0){
                                image = pointList[j].picUrl+'?imageView2/0/w/120';
                            }
                            $("#taskList-"+ (taskId+ "_" + self.getNewId)).append(`
                                <li id="checkApply_`+pointList[j].id+``+taskId+ '_' + self.getNewId + `" data-id="`+pointList[j].visualCode+`" style="padding: 8px 0;">
                                    <a href="#" class="item-link item-content checkApplyList-get-task-list">
                                        <div class="item-inner">
                                            <div class="item-title-row" style="padding-right: 0px;">
                                                <div class="item-title" style="font-weight: bold;width:100%">
                                                <div style="text-overflow: ellipsis;display: inline-block;overflow: hidden;width:60%">`+common.transNullundefinedToline(pointList[j].name)+`</div>
                                                <span class="right-type" style="background-color:`+color+`;float:right">`+common.transNullundefinedToline(pointList[j].pointResultDesc)+`</span>
                                            </div>
                                            </div>
                                            <div class="item-text checkApply-address">
                                                `+common.transNullundefinedToline(pointList[j].address)+`
                                            </div>
                                            <div class="item-text" style="color:#2471CA" id="point_status_`+ pointList[j].id+`` + taskId + `_` + self.getNewId + `">
                                                `+common.transNullundefinedToline(pointList[j].finishTime)+"&nbsp;&nbsp;&nbsp;&nbsp;由"+common.transNullundefinedToline(pointList[j].inspectPointAccount)+`巡检完成
                                            </div>
                                        </div>
                                        <div class="item-media imgbox">
                                            <img src="`+image+`">
                                        </div>
                                    </a>
                                </li>
                            `);

                            //没有完成不显示名称
                            if(pointList[j].status != 2){
                                jQuery("#point_status_"+ pointList[j].id + taskId + "_" + self.getNewId).hide();
                            }
                            
                            jQuery("#checkApply_"+pointList[j].id+""+taskId + "_" + self.getNewId).off('click').on('click',{'taskId':taskId},function(e) {
                                if(jQuery(e.target).hasClass("checkApply-address")){
                                    return;
                                }
                                var visualCode = jQuery(e.currentTarget).attr("data-id");
                                var taskId = e.data.taskId;
                                //判断是否有特殊字符
                                var isHave=visualCode.indexOf("#");
                                var newCode="";
                                if(isHave==0){
                                    newCode=visualCode.substring(1);
                                }else{
                                    newCode=visualCode;
                                }
                                app.router.navigate('/checkCompletedNew/'+isHave+"/"+newCode+'/'+taskId+'/');
                            });  
                        }
                        common.loading(0);
                    }
                });    
            },
            //0：全部1未完成，2已完成
            transIsFinish:function(res){
                if(res==1){
                    return "<span style='background-color:#E07762;color:#fff;font-size: 12px;padding: 2px;border-radius: 3px;'>未完成</span>"
                }else if(res==2){
                    return "<span style='background-color:#69BA71;color:#fff;font-size: 12px;padding: 2px;border-radius: 3px;'>已完成</span>"
                }else{
                    return "";
                }
            },
            loadMoreInspectRecordsList: function () {
                var self = this;
                var $ = self.$$;
                if (self.allowInfinite){
                    if (!self.allowInfinite) return;
                    self.allowInfinite = false;
                    setTimeout(function () {
                        self.historyIndex++;
                        self.getHistoryCheckData();
                    }, 1000);
                }
            },
            refreshCurrentList:function (e, done) {
                var self = this;
                var $ = self.$$;
                if (done && e) {
                    setTimeout(function () {
                        self.historyIndex = 0;
                        $("#history-check-content_" + self.getNewId).empty();
                        $('#inspectRecords_preloader_' + self.getNewId).hide();
                        self.getHistoryCheckData();
                        done();
                    },1000);
                }
            },
            //是否完成筛选
            getTbCompleteInfo:function(){
                var self = this;
                var dateType = [];
                var dateResylt=[];
                dateResylt.push({"id":2,"text":"完成"});
                dateResylt.push({"id":1,"text":"未完成"});
                for (var i = 0;i < dateResylt.length > 0;i++){
                        var obj = {};
                        obj.id= dateResylt[i].id;
                        obj.name= dateResylt[i].text;
                        dateType.push(obj);
                }
                selectOptionFn(dateType,function(data){
                    if(self.complete == data.id){
                        $("#select_hidden_isComplete_" + self.getNewId).removeClass("select-active");
                        jQuery("#select_hidden_isComplete_" + self.getNewId).html("是否完成");
                        self.complete=0;
                    }else{
                        jQuery("#select_hidden_isComplete_" + self.getNewId).html(data.name);
                        $("#select_hidden_isComplete_" + self.getNewId).addClass("select-active");
                        self.complete=data.id;
                    }
                    if(self.selectOptionIndex2 != data.index){
                        self.selectOptionIndex2 = data.index;
                    }else{
                        self.selectOptionIndex2 = "";
                    }
                    self.clearContent();
                    self.getHistoryCheckData();
                },self.selectOptionIndex2);
            },
            //是否合格筛选
            getTbQualifiedInfo:function(){
                var self = this;
                var dateType = [];
                var dateResylt=[];
                dateResylt.push({"id":1,"text":"合格"});
                dateResylt.push({"id":2,"text":"有不合格"});
                for (var i = 0;i < dateResylt.length > 0;i++){
                    var obj = {};
                    obj.id= dateResylt[i].id;
                    obj.name= dateResylt[i].text;
                    dateType.push(obj);
                }
                selectOptionFn(dateType,function(data){
                    if(self.qualified == data.id){
                        $("#select_hidden_isQualified_" + self.getNewId).removeClass("select-active");
                        jQuery("#select_hidden_isQualified_" + self.getNewId).html("是否合格");
                        self.qualified=0;
                    }else{
                        jQuery("#select_hidden_isQualified_" + self.getNewId).html(data.name);
                        $("#select_hidden_isQualified_" + self.getNewId).addClass("select-active");
                        self.qualified=data.id;
                    }
                    if(self.selectOptionIndex1 != data.index){
                        self.selectOptionIndex1 = data.index;
                    }else{
                        self.selectOptionIndex1 = "";
                    }
                    self.clearContent();
                    self.getHistoryCheckData();
                },self.selectOptionIndex1);
            },
            //时间筛选
            getTbTimeInfo:function(){
                var self = this;
                loadSelectedDate_Fn(1,function(data){
                    jQuery("#hidden_date_inspect_" + self.getNewId).empty();
                    var timeArr = data.singleDate.split("  ")[0];
                    jQuery("#hidden_date_inspect_" + self.getNewId).html(timeArr);
                    jQuery("#hidden_date_inspect_" + self.getNewId).addClass('select-active');
                    self.dateTime = timeArr;
                    self.clearContent();
                    self.getHistoryCheckData();
                });
            },
            //清空选项
            removeScreen:function(){
                var self = this;
                self.orgIds = userInfor.organizationIds;
                jQuery("#select_hidden_org_" + self.getNewId).html("选择单位");
                jQuery("#select_hidden_org_" + self.getNewId).removeClass('select-active');
                self.selectOptionOrg = "";

                self.selectOptionIndex = "0";
                self.dateTime = "";
                jQuery("#hidden_date_inspect_" + self.getNewId).html("选择日期");
                jQuery("#hidden_date_inspect_" + self.getNewId).removeClass('select-active');

                $("#select_hidden_isQualified_" + self.getNewId).removeClass("select-active");
                jQuery("#select_hidden_isQualified_" + self.getNewId).html("是否合格");
                self.qualified=0;
                self.selectOptionIndex1="";

                $("#select_hidden_isComplete_" + self.getNewId).removeClass("select-active");
                jQuery("#select_hidden_isComplete_" + self.getNewId).html("是否完成");
                self.complete=0;
                self.selectOptionIndex2="";

                self.clearContent();
                self.getHistoryCheckData();
                common.loading(0);
            },
            clearContent:function() {
                var self = this;
                $("#history-check-content_" + self.getNewId).empty();
                if (userInfor.isMuilUnitUser == 0) {
                    if (self.selectOptionIndex !== ""||self.complete!=0||self.qualified!=0){
                        $("#date_select2_" + self.getNewId).addClass('search-select');
                    } else {
                        $("#date_select2_" + self.getNewId).removeClass('search-select');
                    }
                } else {
                    if (self.selectOptionIndex !== ""||this.orgIds.indexOf(",")<0||self.complete!=0||self.qualified!=0){
                        $("#date_select2_" + self.getNewId).addClass('search-select');
                    } else {
                        $("#date_select2_" + self.getNewId).removeClass('search-select');
                    }
                }
            },
        },
        on: {
            pageInit: function (e, page) {
                var self = this;
                //筛选框选项
                if(userInfor.isMuilUnitUser==0){
                    $("#select_hidden_org_" + self.getNewId).hide();
                }else {
                    $("#select_hidden_org_" + self.getNewId).show();
                }

                // 选择单位
                jQuery("#select_hidden_org_" + self.getNewId).click(function(e) {
                    loadSelectUnitFunc(function(data){
                        //选中
                        if(self.selectOptionOrg != data.orgId){
                            self.selectOptionOrg = data.orgId;
                            self.orgIds=data.orgId;
                            jQuery("#select_hidden_org_" + self.getNewId).html(data.orgName);
                            jQuery("#select_hidden_org_" + self.getNewId).addClass('select-active');
                        } else {
                            self.selectOptionOrg = "";
                            self.orgIds=userInfor.organizationIds;
                            jQuery("#select_hidden_org_" + self.getNewId).html("选择单位");
                            jQuery("#select_hidden_org_" + self.getNewId).removeClass('select-active');
                        }
                        self.clearContent();
                        self.getHistoryCheckData();
                    },self.selectOptionOrg);
                });
                //条件查询
                jQuery("#check_search").keypress(function(event) {
                    if (event.which == 13) {
                        self.selectContent = $("#check_search")[0].value;
                        self.clearContent();
                        self.getHistoryCheckData();
                        $("#check_search").blur();
                    };
                });
                //搜索
                jQuery("#checkApply-search").off().click(function(event) {
                    searchPouper_fn(function(name){
                        self.selectContent = name;
                        self.getHistoryCheckData();
                    },jQuery("#checkApply-search"));
                });

                self.getHistoryCheckData();
            },
            pageBeforeOut: function(e, page) {
                
            },
        },
    }
</script>